var Q = require("q"),
    mongoose = require('mongoose'),
    entityCollection = require('../../../Base/server/controllers/EntityCollection'),
    BaseEnum = require('../../../Base/server/controllers/Enum'),
    Transaction = require('../../../Base/server/controllers/Transaction');


exports.createEntityDataPush = function(req, res) {
    var parameter = req.body;
    var fullname = parameter.FullName,
        data = JSON.parse(parameter.Data)

    var entityName = '';
    var entityRecords = [];


    try {

        if (fullname === 'UFIDA.U9.BC.BaseData.BarCode') {
            entityName = 'BarcodeMaster';
            var batchCount = 0;
            for (var i = 0; i < data.length; i++) {
                var objPushData = data[i];
                var newRecord = {};
                newRecord.ID = -1;
                newRecord.RowStatus = BaseEnum.RowStatus.Insert.Value;
                newRecord.Barcode = objPushData.Code;
                newRecord.ErpID = objPushData.ID;
                // newRecord.EntityID=objPushData.EntityID;
                // newRecord.EntityName=objPushData.EntityID;
                newRecord.ErpBCType = objPushData.BarCodeType;
                newRecord.BCRuleName = objPushData.BCRuleName;
                newRecord.QtyType = objPushData.QtyType;
                newRecord.Qty = objPushData.Qty;
                newRecord.PrintPerson = objPushData.PrintPerson;
                newRecord.IsPrint = objPushData.IsPrint;
                newRecord.Segment1 = objPushData.Segment1;
                newRecord.Segment2 = objPushData.Segment2;
                newRecord.Segment3 = objPushData.Segment3;
                newRecord.Segment4 = objPushData.Segment4;
                newRecord.Segment5 = objPushData.Segment5;
                newRecord.Segment6 = objPushData.Segment6;
                newRecord.Segment7 = objPushData.Segment7;
                newRecord.Segment8 = objPushData.Segment8;
                newRecord.Segment9 = objPushData.Segment9;
                newRecord.Segment10 = objPushData.Segment10;
                entityRecords.push(newRecord);
            }
        } else if (fullname === 'UFIDA.U9.Base.Organization.Organization') {
            entityName = 'Org';

            for (var i = 0; i < data.length; i++) {
                var objPushData = data[i];
                var newRecord = {};
                newRecord.ID = -1;
                newRecord.RowStatus = BaseEnum.RowStatus.Insert.Value;
                newRecord.ErpID = objPushData.ID;
                newRecord.Code = objPushData.Code;
                newRecord.Name = objPushData.Name;
                entityRecords.push(newRecord);
            }
        } else if (fullname == "UFIDA.U9.CBO.SCM.Warehouse.Warehouse") {
            entityName = "StorageLocation";

            for (var i = 0; i < data.length; i++) {
                var newRecord = {};
                var item = data[i];
                newRecord.ID = -1;
                newRecord.RowStatus = BaseEnum.RowStatus.Insert.Value;
                newRecord.ErpID = item.ID;
                newRecord.Code = item.Code;
                newRecord.Name = item.Name;
                newRecord.Org = item.Org;
                newRecord.OrgCode = item.OrgCode;
                newRecord.OrgName = item.OrgName;
                newRecord.Department = item.Department;
                newRecord.DepartmentCode = item.DepartmentCode;
                newRecord.DepartmentName = item.DepartmentName;
                newRecord.IsEffevtive = item.IsEff;
                newRecord.Dsc = item.Dsc;
                entityRecords.push(newRecord);
            }
        } else if (fullname == "UFIDA.U9.CBO.SCM.Bin.Bin") {
            entityName = "Bin";

            for (var i = 0; i < data.length; i++) {
                var newRecord = {};
                var item = data[i];
                newRecord.ID = -1;
                newRecord.RowStatus = BaseEnum.RowStatus.Insert.Value;
                newRecord.ErpID = item.ID;
                newRecord.Code = item.Code;
                newRecord.Name = item.Name;
                newRecord.Org = item.Org;
                newRecord.OrgCode = item.OrgCode;
                newRecord.OrgName = item.OrgName;
                newRecord.Warehouse = item.Warehouse;
                newRecord.WarehouseCode = item.WarehouseCode;
                newRecord.WarehouseName = item.WarehouseName;
                newRecord.IsEffective = item.IsEff;
                newRecord.Dsc = item.Dsc;
                entityRecords.push(newRecord);
            }
        } else if (fullname == "UFIDA.U9.CBO.SCM.Supplier.Supplier") {
            entityName = "Supplier";

            for (var i = 0; i < data.length; i++) {
                var newRecord = {};
                var item = data[i];
                newRecord.ID = -1;
                newRecord.RowStatus = BaseEnum.RowStatus.Insert.Value;
                newRecord.ErpID = item.ID;
                newRecord.Code = item.Code;
                newRecord.Name = item.Name;
                newRecord.Org = item.Org;
                newRecord.OrgName = item.OrgName;
                newRecord.OrgCode = item.OrgCode;
                newRecord.IsEffective = item.IsEff;
                newRecord.Dsc = item.Dsc;
                entityRecords.push(newRecord);
            }
        } else if (fullname == "UFIDA.U9.CBO.SCM.Customer.Customer") {
            entityName = "Customer";

            for (var i = 0; i < data.length; i++) {
                var newRecord = {};
                var item = data[i];
                newRecord.ID = -1;
                newRecord.RowStatus = BaseEnum.RowStatus.Insert.Value;
                newRecord.ErpID = item.ID;
                newRecord.Code = item.Code;
                newRecord.Name = item.Name;
                newRecord.Org = item.Org;
                newRecord.OrgName = item.OrgName;
                newRecord.OrgCode = item.OrgCode;
                newRecord.IsEffective = item.IsEff;
                newRecord.Dsc = item.Dsc;
                entityRecords.push(newRecord);
            }
        }

        var toDeleteList = [];
        entityRecords.forEach(function(item) {
            toDeleteList.push(item.ErpID + "");
        });
        var entityTemp = entityCollection.getEntity(entityName);
        entityTemp.deleteByCondition({ "ErpID": { $in: toDeleteList } }, function(error) {
            if (error) {
                var resError = {};
                resError.IsSuccess = false;
                if (error.message)
                    resError.ErrorMsg = error.message;
                else
                    resError.ErrorMsg = erro

                res.json(resError);
                res.end();
            } else {
                Transaction.BatchSaveByTran([
                        { EntityName: entityName, Records: entityRecords }
                    ],
                    function(err, result) {
                        if (err) {
                            var newErr = {};
                            newErr.IsSuccess = false;
                            newErr.ErrorMsg = '同步数据失败！' + err.message;

                            res.json(newErr);
                        } else {
                            var newRes = {};
                            newRes.IsSuccess = true;
                            newRes.ErrorMsg = '';
                            res.json(newRes);
                        }
                        res.end();
                    }
                );
            }
        });

        //先删除没有错误则进行插入

    } catch (error) {
        var returnError = {};
        returnError.IsSuccess = false;
        returnError.ErrorMsg = "同步数据失败，" + error.message;
        res.json(returnError);
        res.end();
    }

};